﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Environment;

namespace ConfigCreate.Controls
{
    /// <summary>
    /// CreateControlOpenFolder.xaml 的交互逻辑
    /// </summary>
    public partial class CreateControlOpenFolder : System.Windows.Controls.UserControl
    {
        /// <summary>
        /// 选择路径事件
        /// </summary>
        public event EventHandler<EventArgs>? SelectedPathChanged;

        /// <summary>
        /// 
        /// </summary>
        public CreateControlOpenFolder()
        {
            InitializeComponent();
        }

        #region 属性
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public string SelectedPath
        {
            get { return (string)GetValue(SelectedPathProperty); }
            set { SetValue(SelectedPathProperty, value); }
        }
        /// <summary>
        /// 选择文件路径
        /// </summary>
        public static readonly DependencyProperty SelectedPathProperty = DependencyProperty.Register("SelectedPath", typeof(string), typeof(CreateControlOpenFolder), new PropertyMetadata(string.Empty));

        /// <summary>
        /// 对话框标题
        /// </summary>
        public string Description
        {
            get { return (string)GetValue(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }
        /// <summary>
        /// 对话框标题
        /// </summary>
        public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(CreateControlOpenFolder), new PropertyMetadata("请选择目录"));

        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }
        /// <summary>
        /// 其中文本框是否为只读模式
        /// </summary>
        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(CreateControlOpenFolder), new PropertyMetadata(false));

        /// <summary>
        /// 是否显示新建目录按钮
        /// </summary>
        public bool ShowNewFolderButton
        {
            get { return (bool)GetValue(ShowNewFolderButtonProperty); }
            set { SetValue(ShowNewFolderButtonProperty, value); }
        }
        /// <summary>
        /// 是否显示新建目录按钮
        /// </summary>
        public static readonly DependencyProperty ShowNewFolderButtonProperty = DependencyProperty.Register("ShowNewFolderButton", typeof(bool), typeof(CreateControlOpenFolder), new PropertyMetadata(true));

        /// <summary>
        /// 打开对话框的根目录
        /// </summary>
        public SpecialFolder RootFolder
        {
            get { return (SpecialFolder)(GetValue(RootFolderProperty)); }
            set { SetValue(RootFolderProperty, value); }
        }
        /// <summary>
        /// 打开对话框的根目录
        /// </summary>
        public static readonly DependencyProperty RootFolderProperty = DependencyProperty.Register("RootFolder", typeof(SpecialFolder), typeof(CreateControlOpenFolder), new PropertyMetadata(SpecialFolder.Desktop));
        #endregion

        private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fb = new System.Windows.Forms.FolderBrowserDialog();
            fb.Description = Description;
            fb.RootFolder = RootFolder;
            fb.SelectedPath = SelectedPath;
            fb.ShowNewFolderButton = ShowNewFolderButton;
            if (fb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SelectedPath = fb.SelectedPath;
                SelectedPathChanged?.Invoke(this, EventArgs.Empty);
            }
        }
    }
}
